package ym.controller.back.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.Permission;
import ym.service.PermissionService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台权限接口
 */
@CrossOrigin
@RestController
@RequestMapping("/back/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 新增权限
     *
     * @param permission
     * @return 执行结果
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/sys')")//springsecurity鉴权注解
    public BaseResult add(Permission permission) {
        permissionService.add(permission);
        return BaseResult.ok();
    }


    /**
     * 修改权限
     *
     * @param permission
     * @return 执行结果
     */
    @PostMapping("/update")
    public BaseResult update(Permission permission) {
        permissionService.update(permission);
        return BaseResult.ok();
    }


    /**
     * 删除权限
     *
     * @param permissionId
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer permissionId) {
        permissionService.delete(permissionId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询权限
     *
     * @param permissionId
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer permissionId) {
        Permission permission = permissionService.findById(permissionId);
        return BaseResult.ok(permission);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<Permission> all = permissionService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询权限
     *
     * @param search 搜索条件
     * @param page   当前页
     * @param size   每页大小
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/sys')")
    public BaseResult search(String search, int page, int size) {
        Page<Permission> permissionPage = permissionService.search(search, page, size);
        return BaseResult.ok(permissionPage);
    }


}
